<?php


namespace App\Repositories\Policy\Eloquent;


use Carbon\Carbon;
use Curl\CurlService\CurlService;
use Illuminate\Support\Facades\Cache;

class PolicyRepository
{
    protected $policyApiBaseUrl = 'https://exchdata.ctrip.com/Flight-Proxy-TradeQuickAPI/api/json/';    //特殊政策
    protected $lowPriceApiBaseUrl = 'https://apiproxy.ctrip.com/apiproxy/soa2/11576/json/';             //低价查询
    protected $ctripRuleAipBaseUrl = 'http://exchdata.ctrip.com/Flight-Proxy-TradeQuickAPI/api/json/';  //规则运价

    protected $MessageHead;
    protected $MessageHeadHB;
    protected $MessageBody;

    protected $time = 0;

    public function __construct(){

        $this->initMessageHead();

        $this->MessageBody = [
            "StartIndex" => 0,
            "PageSize" => 100,
            "SearchType" => "ByCondition",
            "SearchCondition" => [
                "PolicySource" => "All",
            ],
        ];
    }

    /**
     * 初始化头部
     */
    protected function initMessageHead(){

        $this->time = intval(sprintf('%.3f',microtime(true))*1000);

        //最慧平台
        $this->MessageHead = [

            "UserInfo"=>[
                "UserName"=>config('env.ctrip_username'),
                "Password"=>md5(config('env.ctrip_username')."#".config('env.ctrip_pwd')),
            ],

            "RequestGUID"=> self::uuid(),
            "ClientID"=>5,
            "ClientIP"=>"",
            "RequestTime"=>"/Date(".$this->time."+0800)/"
        ];

        //恒博平台
        $this->MessageHeadHB = [

            "UserInfo"=>[
                "UserName"=>config('env.ctrip_hb_username'),
                "Password"=>md5(config('env.ctrip_hb_username')."#".config('env.ctrip_hb_pwd')),
            ],

            "RequestGUID"=> self::uuid(),
            "ClientID"=>5,
            "ClientIP"=>"",
            "RequestTime"=>"/Date(".$this->time."+0800)/"
        ];

        //恒博平台
        $this->MessageHeadMh = [

            "UserInfo"=>[
                "UserName"=>config('env.ctrip_mh_username'),
                "Password"=>md5(config('env.ctrip_mh_username')."#".config('env.ctrip_mh_pwd')),
            ],

            "RequestGUID"=> self::uuid(),
            "ClientID"=>5,
            "ClientIP"=>"",
            "RequestTime"=>"/Date(".$this->time."+0800)/"
        ];
    }

    /**
     * 根据航司选择不通的账号
     * @param $hsCode
     * @return mixed
     */
    protected function getMessageHead($hsCode){

        if (in_array($hsCode,['HU','JD','MF','DZ'])) {

            return $this->MessageHead;
        } else {

            return $this->MessageHeadHB;
        }

    }

    /**
     * 生成唯一性的uuid
     * @return string
     */
    public static function  uuid()
    {
        $chars = md5(uniqid(mt_rand(), true));
        $uuid = substr ( $chars, 0, 8 ) . '-'
            . substr ( $chars, 8, 4 ) . '-'
            . substr ( $chars, 12, 4 ) . '-'
            . substr ( $chars, 16, 4 ) . '-'
            . substr ( $chars, 20, 12 );
        return $uuid ;
    }

    /**
     * 请求返回值
     * @param $apiId
     * @param array $param
     * @param int $isCache
     * @return mixed
     */
    public function req($apiId,$param = [],$isCache = 0){
        $url = $this->policyApiBaseUrl . $apiId;

        //改变requestGUID
        $param['MessageHead']['RequestGUID'] = self::uuid();

        return $this->curlRequest($url,$apiId,$param,$isCache);
    }

    /**
     * 低价政策推荐
     * @param $apiId
     * @param array $param
     * @param int $isCache
     */
    public function lowPriceReq($apiId,$param = [],$isCache = 0){

        $url = $this->lowPriceApiBaseUrl . $apiId;
        return $this->curlRequest($url,$apiId,$param,$isCache);
    }

    /**
     * 规则运价请求接口
     * @param $apiId
     * @param array $param
     * @param int $isCache
     * @return mixed
     */
    public function ruleReq($apiId,$param = [],$isCache = 0) {

        $url = $this->ctripRuleAipBaseUrl . $apiId;

        //改变requestGUID
        $param['MessageHead']['RequestGUID'] = self::uuid();

        return $this->curlRequest($url,$apiId,$param,$isCache);
    }

    /**
     * 数据查询
     * @param $apiId
     * @param array $param
     * @param int $isCache
     * @return mixed
     */
    private function curlRequest($url,$apiId,$param = [],$isCache = 0){

        $res = CurlService::post($url,$param);
        //结果是否要存redis
        if ($isCache) {

            Cache::put($apiId,json_encode($res),Carbon::now()->addMinutes(1));
        }

        return $res;
    }
}